<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
  <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  <title></title>
</head>

<body>
  <div style="text-align: right;">请点击右上角选择发送给朋友 ↑</div>
</body>
<script>
  // jsSDK初始化
  function init(data, title, desc, link, imgUrl) {
    wx.config({
      appId: data.appId,
      timestamp: data.timestamp * 1,
      nonceStr: data.nonceStr,
      signature: data.signature,
      jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData']
    })

    wx.ready(function () {
      wx.updateAppMessageShareData({
        title, // 分享标题
        desc, // 分享描述
        link: `${window.location.origin + window.location.pathname}?isPath=${encodeURIComponent(link)}`,
        imgUrl, // 分享图标
        success: function () {
          console.log('分享调用成功！')
        }
      })
    })
  }

  // 获取参数
  function GetRequest() {
    var url = location.search;
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      strs = str.split("&");
      for (var i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  }

  var request = new Object();
  request = GetRequest();
  console.log(request)

  if (!request.isPath) {
    $.ajax({
      url: "http://api-mall.test.tomorni.com/wechat/getJsApiTicket",
      data: {
        link: window.location.href
      },
      success: function (res) {
        init(res.data, request.title, request.desc, request.link, request.imgUrl)
      }
    });
  } else {
    window.location.href = decodeURIComponent(request.isPath)
  }


</script>

</html>